//
//  BuyViewController.m
//  OverseasOnlineShopping
//
//  Created by 唐先才 on 16/3/7.
//  Copyright © 2016年 唐先才. All rights reserved.
//

#import "BuyViewController.h"
#import "AFNetworking.h"
#import "OverseasOnlineShopping-Swift.h"
#import "TxcModel.h"
#import "TxcPrefixHeader.pch"
#import "MJRefresh.h"
#import "MJRefreshHeader.h"
#import "MJRefreshFooter.h"
#import "SVProgressHUD.h"

@interface BuyViewController ()
{
    int _page;
    BOOL _isPulling;
    WaitLoadView *_waitView;
}
@end
@implementation BuyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BACKCOLOR;
    _page = 1;
    [self createData];
    [self createRefresh];
    WaitLoadView *waitView = [[WaitLoadView alloc] initWithFrame: [UIScreen mainScreen].bounds];
    _waitView = waitView;
    [self.view addSubview:waitView];
}

- (void)createData {
    if (_isPulling) {
        [self.baseDataArray removeAllObjects];
    }
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *page = [NSString stringWithFormat:@"%d", _page];
    NSDictionary *parameters = @{@"brightUrl": @"",
                                @"catUrl": @"",
                                @"exceptMallUrl": @"",
                                @"homeShow": @"0",
                                @"infoNation": @"0",
                                @"infoType": @"5",
                                @"isTop": @"1",
                                @"keyWord": @"",
                                @"mallUrl": @"",
                                @"pageIndex": page,
                                @"pageSize": @"20",
                                @"tagUrl": @""
                                 };
    [manager POST:BUY_URL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [_waitView removeFromSuperview];
        NSArray *array = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        for (NSDictionary *dict in array) {
            TxcModel *model = [TxcModel modelWithDict:dict];
            [self.baseDataArray addObject:model];
        }
        [self.baseTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error===%@", error);
        [SVProgressHUD showErrorWithStatus:AppNetErrorTips];
    }];
}

- (void)createRefresh {
    self.baseTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downRefresh)];
    self.baseTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(upRefresh)];
}

- (void)downRefresh {
    _isPulling = YES;
    _page = 1;
    [self.baseTableView.mj_header beginRefreshing];
    [self createData];
    [self.baseTableView.mj_header endRefreshing];
}

- (void)upRefresh {
    _isPulling = NO;
    _page ++;
    [self.baseTableView.mj_footer beginRefreshing];
    [self createData];
    [self.baseTableView.mj_footer endRefreshing];
}

@end
